package com.yf.system.modules.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.BaseIdsReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.notify.modules.notify.dto.MsgDTO;
import com.yf.notify.modules.notify.dto.request.MsgQueryDTO;
import com.yf.notify.modules.notify.service.MsgReadService;
import com.yf.notify.modules.notify.service.MsgService;
import com.yf.system.modules.user.UserUtils;
import com.yf.system.modules.user.dto.request.UserMsgReqDTO;
import com.yf.system.modules.user.service.UserMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 管理用户控制器
 * </p>
 *
 * @author 聪明笨狗
 * @since 2020-04-13 16:57
 */
@Api(tags = {"用户消息"}, consumes = "查找学员端的站内信息")
@RestController
@Log4j2
@RequestMapping("/api/sys/user/msg")
public class SysUserMsgController extends BaseController {

    @Autowired
    private MsgService msgService;

    @Autowired
    private UserMsgService userMsgService;

    @Autowired
    private MsgReadService msgReadService;


    /**
     * 添加或修改
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "手动发送消息")
    @RequiresPermissions("notify:msg:send")
    @PostMapping("/manual-send")
    public ApiRest send(@RequestBody UserMsgReqDTO reqDTO) {
        userMsgService.sendMsg(reqDTO);
        return super.success();
    }


    /**
     * 用户站内信
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "分页查找")
    @PostMapping("/paging")
    public ApiRest<IPage<MsgDTO>> paging(@RequestBody PagingReqDTO<MsgQueryDTO> reqDTO) {

        // 修正查询参数
        MsgQueryDTO params = reqDTO.getParams();
        if(params == null){
            params = new MsgQueryDTO();
        }
        params.setUserId(UserUtils.getUserId());
        if(params.getReadState()==null){
            params.setReadState(0);
        }
        reqDTO.setParams(params);

        //分页查询并转换
        IPage<MsgDTO> page = msgService.userPaging(reqDTO);
        return super.success(page);
    }


    /**
     * 查找详情用于阅读
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找详情用于阅读", notes = "调用此接口自动标记为已读")
    @PostMapping("/detail-for-read")
    public ApiRest<MsgDTO> detail(@RequestBody BaseIdReqDTO reqDTO) {
        MsgDTO dto = msgService.detailForRead(reqDTO.getId(), UserUtils.getUserId());
        return super.success(dto);
    }

    /**
     * 统计未读消息数量
     * @return
     */
    @ApiOperation(value = "统计未读消息数量", notes = "统计未读消息数量")
    @PostMapping("/count-unread")
    public ApiRest countUnread() {

        int count = msgReadService.countUnread(UserUtils.getUserId());
        Map<String,Object> map = new HashMap<>(1);
        map.put("count", count);
        return super.success(map);
    }



    /**
     * 标记为已读
     * @return
     */
    @ApiOperation(value = "标记为已读", notes = "标记为已读")
    @PostMapping("/mark-read")
    public ApiRest markRead(@RequestBody BaseIdsReqDTO reqDTO) {
        msgReadService.markAsRead(UserUtils.getUserId(), reqDTO.getIds());
        return super.success();
    }


    /**
     * 用户删除站内信
     * @return
     */
    @ApiOperation(value = "用户删除站内信", notes = "用户删除站内信")
    @PostMapping("/user-delete")
    public ApiRest userDelete(@RequestBody BaseIdsReqDTO reqDTO) {
        msgReadService.userDelete(UserUtils.getUserId(), reqDTO.getIds());
        return super.success();
    }
}
